package de.java.shortys.myhome.fuelmonitor.view;

import static de.java.shortys.myhome.api.images.ImageConstants.ICON_DELTE;
import static de.java.shortys.myhome.api.images.ImageConstants.ICON_EDIT;
import static de.java.shortys.myhome.fuelmonitor.controller.FuelMonitorController.NAME;
import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.AMOUNT;
import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.AMOUNT_SYMBOL;
import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.CHARGES;
import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.DATE;
import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.DELETE;
import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.EDIT;
import static de.java.shortys.myhome.language.controller.LanguageManager.getInstance;
import static java.awt.Adjustable.HORIZONTAL;
import static java.awt.Adjustable.VERTICAL;
import static java.awt.BorderLayout.NORTH;
import static java.awt.FlowLayout.RIGHT;
import static javax.swing.ListSelectionModel.SINGLE_SELECTION;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Observable;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import de.java.shortys.myhome.api.view.AbstractSwingSubModuleView;
import de.java.shortys.myhome.api.view.SwingModuleViewScrollBarUI;
import de.java.shortys.myhome.fuelmonitor.controller.FuelMonitorController;
import de.java.shortys.myhome.fuelmonitor.model.IFuelFilling;
import de.java.shortys.myhome.fuelmonitor.model.IFuelMonitorModelService;
import de.java.shortys.myhome.language.controller.LanguageManager;
import de.java.shortys.utils.format.FormatUtils;
import de.java.shortys.utils.jtable.CalendarCellRenderer;
import de.java.shortys.utils.jtable.JTableUtils;

public class SubModuleViewOverview extends AbstractSwingSubModuleView {

	private static final String COLUMNNAME_KEY = "key";
	private static final String COLUMNNAME_DATE = getInstance(NAME).get(DATE);
	private static final String COLUMNNAME_AMOUNT = getInstance(NAME).get(AMOUNT) + " ("
			+ getInstance(NAME).get(AMOUNT_SYMBOL) + ")";
	private static final String COLUMNNAME_CHARGES = getInstance(NAME).get(CHARGES);
	private static Vector<String> columns;

	static {
		columns = new Vector<String>();
		columns.add(COLUMNNAME_KEY);
		columns.add(COLUMNNAME_DATE);
		columns.add(COLUMNNAME_AMOUNT);
		columns.add(COLUMNNAME_CHARGES);
	}

	private JToolBar toolbar;
	private JButton toolbarItemEdit;
	private JButton toolbarItemDelete;

	private JScrollPane tableScrollPane;
	private JTable table;

	private DefaultTableModel tableModel;

	public SubModuleViewOverview() {
		initialize();
	}

	public void addDeleteListener(ActionListener listener) {
		toolbarItemDelete.addActionListener(listener);
	}

	public void addEditListener(ActionListener listener) {
		toolbarItemEdit.addActionListener(listener);
	}

	public void addDoubleClickListener(MouseListener listener) {
		table.addMouseListener(listener);
	}

	private void addRow(IFuelFilling fuelFilling) {
		Locale currentLocale = LanguageManager.getInstance(FuelMonitorController.NAME).getCurrentLocale();
		String charges = FormatUtils.getCurrencyOutput(currentLocale, fuelFilling.getCharges());
		Calendar dateFilled = fuelFilling.getDateFilled();
		String key = fuelFilling.getKey();
		String amount = FormatUtils.getDoubleOutput(currentLocale, fuelFilling.getAmount(), 1);
		tableModel.addRow(new Object[] { key, dateFilled, amount, charges });
	}

	@Override
	public void clear() {
		tableModel.setRowCount(0);
	}

	private void configureColumns() {
		TableColumn columnKey = table.getColumn(COLUMNNAME_KEY);
		JTableUtils.setColumnHidden(table, columnKey);

		TableColumn columnDate = table.getColumn(COLUMNNAME_DATE);
		columnDate.setCellRenderer(new CalendarCellRenderer(getInstance(NAME).getCurrentLocale()));
	}

	private void createTable() {
		tableModel = new DefaultTableModel(columns, 0) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		table = new JTable(tableModel);
		table.setBorder(null);
		table.setRowHeight(25);
		table.setSelectionMode(SINGLE_SELECTION);

		configureColumns();
		tableScrollPane = new JScrollPane(table);
		tableScrollPane.getVerticalScrollBar().setUI(new SwingModuleViewScrollBarUI(VERTICAL));
		tableScrollPane.getHorizontalScrollBar().setUI(new SwingModuleViewScrollBarUI(HORIZONTAL));
		tableScrollPane.setBorder(null);
		tableScrollPane.setOpaque(false);
		getContentArea().add(tableScrollPane);
	}

	private void createToolbar() {
		toolbar = new JToolBar();
		toolbar.setLayout(new FlowLayout(RIGHT));
		toolbar.setFloatable(false);
		toolbar.setBorder(null);
		toolbar.setFocusable(false);
		toolbar.setBorderPainted(false);

		toolbarItemEdit = new JButton(LanguageManager.getInstance(FuelMonitorController.NAME).get(EDIT), ICON_EDIT);
		toolbarItemEdit.setEnabled(false);
		toolbar.add(toolbarItemEdit);

		toolbarItemDelete = new JButton(LanguageManager.getInstance(FuelMonitorController.NAME).get(DELETE), ICON_DELTE);
		toolbarItemDelete.setEnabled(false);
		toolbar.add(toolbarItemDelete);
		getContentArea().add(NORTH, toolbar);
	}

	public JTable getTable() {
		return table;
	}

	public JButton getToolbarItemDelete() {
		return toolbarItemDelete;
	}

	public JButton getToolbarItemEdit() {
		return toolbarItemEdit;
	}

	private void initialize() {
		getContentArea().setLayout(new BorderLayout());
		createToolbar();
		createTable();
	}

	public void put(List<IFuelFilling> list) {
		clear();
		for (IFuelFilling fuelFilling : list) {
			addRow(fuelFilling);
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		clear();
		IFuelMonitorModelService fuelMonitorModelService = (IFuelMonitorModelService) o;
		put(fuelMonitorModelService.read());
	}

}
